package com.huanong.it.xwcommon.sentinel;


import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.stereotype.Component;

@Component
public class GlobalDefaultFallback implements BlockExceptionHandler {

    @Override
    public void handle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse
            , BlockException e) throws Exception {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        if(e instanceof FlowException){
            httpServletResponse.getWriter().write("{\"code\":6000,\"data\":null,\"msg\":\"服务器繁忙限流，请稍后再试\"}");
        }
        if(e instanceof DegradeException){
            httpServletResponse.getWriter().write("{\"code\":6000,\"data\":null,\"msg\":\"服务器繁忙熔断，请稍后再试\"}");
        }
        //https://www.zhihu.com/question/605411044/answer/3262894235
    }
}
